package com.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;

/*
* 模拟一个消费者
* */
public class Client {
    public static void main(String[] args) {
        /*
        * 获得委托类
        * */
        final Producer producer=new Producer();

        Producer cglibProducr=(Producer) Enhancer.create(producer.getClass(), new MethodInterceptor() {

            public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                //提供增强的代码
                Object returnValue = null;
                //1.获取方法执行的参数
                Float money = (Float)args[0];
                //2.判断当前方法是不是销售
                if("saleProduct".equals(method.getName())) {
                    returnValue = method.invoke(producer, money*0.8f);
                }
                return returnValue;
            }
        });
        cglibProducr.saleProduct(12000f);

    }
}
